from pickle import TRUE
import py
import pygame
import random
import math

# Constances
SCREEN_SIZE_WIDTH = 800
SCREEN_SIZE_HIGH = 600

PLAYER_SIZE = 64
ENEMY_SIZE = 48

PLAYER_MOVING_SPEED = 0.3
BULLET_MOVING_SPEED = 0.5
ENEMY_DOWNING_SPEED = 20
ENEMY_FLOATING_SPEED = 0.5


# Initialize the pygame
pygame.init()

# Create the screen
screen = pygame.display.set_mode((SCREEN_SIZE_WIDTH, SCREEN_SIZE_HIGH))

# Background
background = pygame.image.load('background.png')
# Background Sound
pygame.mixer.music.load('background.wav')
pygame.mixer.music.play(-1)

# Caption and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('space.png')
pygame.display.set_icon(icon)

# Player
playerImg = pygame.image.load('player.png')
playerX = (SCREEN_SIZE_WIDTH-PLAYER_SIZE) // 2
playerY = SCREEN_SIZE_HIGH-PLAYER_SIZE * 2
playerX_change = 0
playerY_change = 0

# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6

for i in range(num_of_enemies):
    enemyImg.append(pygame.image.load('enemy.png'))
    enemyX.append(random.randint(0, SCREEN_SIZE_WIDTH - ENEMY_SIZE))
    enemyY.append(random.randint(ENEMY_SIZE, ENEMY_SIZE*2))
    enemyX_change.append(ENEMY_FLOATING_SPEED)
    enemyY_change.append(ENEMY_DOWNING_SPEED)

# Bullet
# ready - you can't see the bullet on the screen
# fire - The bullet is currently moving
bulletImg = pygame.image.load('bullet.png')
bulletX = 0
bulletY = SCREEN_SIZE_HIGH-PLAYER_SIZE * 2
bulletX_change = 0
bulletY_change = BULLET_MOVING_SPEED
bullet_state = 'ready'

score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)
textX = 10
textY = 10


# Game over text
over_font = pygame.font.Font('freesansbold.ttf', 64)


def show_score(x, y):
    score = font.render('Score: ' + str(score_value), True, (255, 255, 255))
    screen.blit(score, (x, y))


def game_over_text():
    over_text = over_font.render('GAME OVER', True, (255, 255, 255))
    screen.blit(over_text, (200, 250))


def player(x, y):
    screen.blit(playerImg, (x, y))


def enemy(x, y, i):
    screen.blit(enemyImg[i], (x, y))


def fire_bullet(x, y):
    global bullet_state
    bullet_state = 'fire'
    screen.blit(bulletImg, (x+16, y+10))


def isCollision(enemyX, enemyY, bulletX, bulletY):
    distance = math.sqrt(math.pow(enemyX-bulletX, 2) +
                         math.pow(enemyY - bulletY, 2))
    return distance < 27


# Game Loop
running = True
while running:
    # RGB - Red, Gree, Blue
    screen.fill((0, 0, 0))
    # Background Image
    screen.blit(background, (0, 0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # if keystroke is pressed check whether it's right or left
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change -= PLAYER_MOVING_SPEED
            elif event.key == pygame.K_RIGHT:
                playerX_change += PLAYER_MOVING_SPEED
            elif event.key == pygame.K_UP:
                playerY_change -= PLAYER_MOVING_SPEED
            elif event.key == pygame.K_DOWN:
                playerY_change += PLAYER_MOVING_SPEED
            elif event.key == pygame.K_SPACE:
                if bullet_state == 'ready':
                    pygame.mixer.Sound('laser.wav').play()
                    # Get the current x cordinate of the spaceship
                    bulletX = playerX
                    fire_bullet(bulletX, bulletY)
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                playerY_change = 0

    # print(playerX_change, playerY_change)
    playerX += playerX_change
    if playerX <= 0:
        playerX = 0
    if playerX >= SCREEN_SIZE_WIDTH-PLAYER_SIZE:
        playerX = SCREEN_SIZE_WIDTH-PLAYER_SIZE
    playerY += playerY_change
    if playerY <= 0:
        playerY = 0
    if playerY >= SCREEN_SIZE_HIGH-PLAYER_SIZE:
        playerY = SCREEN_SIZE_HIGH-PLAYER_SIZE

    # Bullet Movement
    if bullet_state is 'fire':
        fire_bullet(bulletX, bulletY)
        bulletY -= bulletY_change
    if bulletY <= 0:
        bulletY = SCREEN_SIZE_HIGH-PLAYER_SIZE * 2
        bullet_state = 'ready'

    # Enemy Movement
    for i in range(num_of_enemies):
        # Game Over
        if enemyY[i] > 200:
            for j in range(num_of_enemies):
                enemyY[j] = 2000
            game_over_text()
            break
        enemyX[i] += enemyX_change[i]
        if enemyX[i] <= 0 or enemyX[i] >= SCREEN_SIZE_WIDTH - ENEMY_SIZE:
            enemyX_change[i] = -enemyX_change[i]
            enemyY[i] += enemyY_change[i]

        # Collision detection
        collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
        if collision:
            pygame.mixer.Sound('explosion.wav').play()
            bulletY = SCREEN_SIZE_HIGH-PLAYER_SIZE * 2
            bullet_state = 'ready'
            score_value += 1
            enemyX[i] = random.randint(0, SCREEN_SIZE_WIDTH - ENEMY_SIZE)
            enemyY[i] = random.randint(ENEMY_SIZE, ENEMY_SIZE * 2)
        enemy(enemyX[i], enemyY[i], i)

    player(playerX, playerY)

    show_score(textX, textY)

    pygame.display.update()
